package com.fangzhi.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fangzhi.mobel.Commission;
import com.fangzhi.mobel.Orders;
import com.fangzhi.mobel.vo.CommissionTaskNewVo;
import com.fangzhi.service.AdminAccountInfoService;
import com.fangzhi.service.CommissionService;
import com.fangzhi.service.OrderProductsService;
import com.fangzhi.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.Arrays;
import java.util.List;

@Component
public class CommissionTaskNew {


    @Autowired
    private OrdersService ordersService;
    @Autowired
    private AdminAccountInfoService adminAccountInfoService;
    @Autowired
    private CommissionService commissionService;

    /**
     * 5分钟更新一次
     * 更新订单佣金
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
//    @PostConstruct
    public void start(){
        LocalDate start = YearMonth.now().atDay(1);
        LocalDate end = start.withDayOfMonth(start.lengthOfMonth());
        List<CommissionTaskNewVo> voList = adminAccountInfoService.queryCommission();
        for (CommissionTaskNewVo commissionTaskNewVo : voList) {
            List<Orders> ordersList = ordersService.list(Wrappers.<Orders>lambdaQuery()
                    .eq(Orders::getAdminAccountId, commissionTaskNewVo.getAccountId())
                    .between(Orders::getCtime, start, end)
                    .in(Orders::getStatus, Arrays.asList(3, 4, 5)));
            if (ordersList.isEmpty())
                continue;

            int totalAmount = ordersList.stream().mapToInt(T-> T.getTotalAmount() - T.getDiscountAmount()).sum();
            int commission;
            if (commissionTaskNewVo.getDeptId() == 7L || commissionTaskNewVo.getDeptId() == 8L){
                commission = getCommissionForCD(totalAmount);
            }else{
                commission = getCommission(totalAmount);
            }

            Commission one = commissionService.getOne(Wrappers.<Commission>lambdaQuery()
                    .eq(Commission::getAccountId, commissionTaskNewVo.getAccountId())
                    .eq(Commission::getMonth, start));
            if (null == one){
                one = new Commission().setAccountId(commissionTaskNewVo.getAccountId())
                        .setMonth(YearMonth.now().atDay(1)).setCommissionPrice(commission).setPrice(commission);
            }else{
                one = one.setAccountId(commissionTaskNewVo.getAccountId())
                        .setMonth(YearMonth.now().atDay(1)).setCommissionPrice(commission).setPrice(commission);
            }
            commissionService.saveOrUpdate(one);
        }
    }

    /**
     * CD两组提成计算方法
     * @param total
     * @return
     */
    public int getCommissionForCD(Integer total) {
        BigDecimal commission = BigDecimal.valueOf(0);
        if (total >= 13000000){
            total = total - 2000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.1));
        }else if(total >= 8000000){
            total = total - 2000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.07));
        }else if(total >= 3000000){
            total = total - 2000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.05));
        }
        return commission.intValue();
    }

    /**
     * 计算其他部门提成
     * @param total
     * @return
     */
    public int getCommission(Integer total) {
        BigDecimal commission = BigDecimal.valueOf(0);
        if (total >= 13000000){
            total = total - 3000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.1));
        }else if(total >= 8000000){
            total = total - 3000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.07));
        }else if(total >= 3000000){
            total = total - 3000000;
            commission = BigDecimal.valueOf(total).multiply(BigDecimal.valueOf(0.05));
        }
        return commission.intValue();
    }
}
